package za.co.chris.traintimes.database;

import static za.co.chris.traintimes.database.DatabaseConstants.DATABASE_NAME;
import static za.co.chris.traintimes.database.DatabaseConstants.DATABASE_VERSION;

import java.util.List;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class TrainTimesDatabase extends SQLiteOpenHelper {

	private final LineFinder lineFinder;

	TrainTimesDatabase(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		lineFinder = new LineFinder(context);

	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		List<String> scripts = lineFinder.makeLineCreateScripts();
		for (String script : scripts) {
			db.execSQL(script);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

		List<String> tableNames = lineFinder.getTables();
		for (String tableName : tableNames) {
			db.execSQL("DROP TABLE IF EXISTS " + tableName);
		}

		onCreate(db);
	}

}
